#ifdef VERTEX
uniform mat4 uProjMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
uniform mat4 uViewProjMatrix;

layout(location = 0) in vec4 aPosition;
layout(location = 1) in vec2 aTexCoord;
layout(location = 2) in vec4 aNormal;
layout(location = 3) in vec4 aColor;

out vec2 vTexCoord;
out vec4 vPosition;
out float vDistance;

void main()
{
	vec4 worldPos = uModelMatrix * aPosition;
	vec4 position = uViewProjMatrix * worldPos;
	vTexCoord = aTexCoord;

#ifdef ENABLE_POINTLIGHT
	vec4 viewPos = uViewMatrix * worldPos;
	vDistance = length(viewPos.xyz);
#else
	vPosition = position;
#endif
	gl_Position = position;
}
#endif

#ifdef FRAGMENT
uniform sampler2D uAlphaMap;

in vec2 vTexCoord;
in vec4 vPosition;
in float vDistance;
out vec4 fragColor;

void main()
{
	if (texture(uAlphaMap, vTexCoord).a < 0.5) discard;

#ifdef ENABLE_POINTLIGHT
	const float bias = 0.02;
	float result = vDistance + bias;
#else
#ifdef ENABLE_DIRECTIONALLIGHT
	const float bias = 0.002;
#else
	const float bias = 0.0001;
#endif
	float depth = vPosition.z / vPosition.w;
	float result = depth * 0.5 + 0.5 + bias;
#endif
	fragColor = vec4(result, result, result, 1.0);
}
#endif